
from pymongo import MongoClient



class MongoTest(object):
    def __init__(self):
        self.client = MongoClient('mongodb://127.0.0.1:27017/')     # 链接
        self.db = self.client['mt']   # 数据库名称

    @staticmethod
    def cursor_to_list(cursor):   # cursor_to_list，mongoDB数据集合 转为 List
        rest = []
        for cr in cursor:
            rest.append(cr)
        return rest

    def add_one(self, collection_name, data):     # collection_name，集合名
        """添加数据"""
        collection = self.db[collection_name]
        return collection.insert_one(data)

    def find_all(self, collection_name):
        """查询该集合中所有的数据"""
        collection = self.db[collection_name]
        result = collection.find()
        return self.cursor_to_list(result)

    def find_one_by_poi_id_from_food(self, poi_id):
        """用 poi_id 获取 mt_food 集合中的信息"""
        collection = self.db['mt_detail']
        result = collection.find_one({'detail.data.poi_info.id': poi_id})
        return result

    def find_one_by_poi_id_from_phone(self, poi_id):
        """用 poi_id 获取 mt_phone 集合中的信息"""
        collection = self.db['mt_contact']
        result = collection.find_one({'contact.data.id': poi_id})
        return result

    def find_page(self, collection_name, i):
        """查询该集合中所有的数据"""
        collection = self.db[collection_name]
        result = collection.find().skip(i * 5).limit(5)
        return self.cursor_to_list(result)

if __name__ == '__main__':
    mongo = MongoTest()
    res = mongo.find_page("anly_data", 50)
    pass